package cn.mumzone.pos.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.jdsjlzx.ItemDecoration.SpacesItemDecoration;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.mumzone.basic.base.BaseActivity;
import cn.mumzone.basic.util.AppDateUtil;
import cn.mumzone.basic.util.DisplayUtil;
import cn.mumzone.basic.util.PosToast;
import cn.mumzone.basic.util.ToastUtil;
import cn.mumzone.pos.AppConstants;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.OutWarehouseDetailEntity;
import cn.mumzone.pos.model.WarehouseDetailEntity;
import cn.mumzone.pos.presenter.WarehouseDetailPresenter;
import cn.mumzone.pos.presenter.WarehouseOutDetailPresenter;
import cn.mumzone.pos.presenter.contract.WareHouseDetailContract;
import cn.mumzone.pos.presenter.contract.WareHouseOutDetailContract;
import cn.mumzone.pos.ui.adapter.warehouse.ADA_WarehouseGoodsList;
import cn.mumzone.pos.ui.adapter.warehouse.ADA_WarehouseGoodsList_Out;

/**
 * Created By Dlx
 * on 2019/10/8
 */
public class ACT_WarehouseDetail_Out extends BaseActivity<WarehouseOutDetailPresenter> implements WareHouseOutDetailContract.View {
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_order)
    TextView tvOrder;
    @BindView(R.id.tv_ticket_num_title)
    TextView tvTicketNumTitle;
    @BindView(R.id.tv_ticket_num)
    TextView tvTicketNum;
    @BindView(R.id.tv_warehouse_type_name)
    TextView tvWarehouseTypeName;
    @BindView(R.id.tv_warehouse_type_content)
    TextView tvWarehouseTypeContent;
    @BindView(R.id.tv_warehouse_title)
    TextView tvWarehouseTitle;
    @BindView(R.id.tv_warehouse_content)
    TextView tvWarehouseContent;
    @BindView(R.id.tv_date_title)
    TextView tvDateTitle;
    @BindView(R.id.tv_date_content)
    TextView tvDateContent;
    @BindView(R.id.tv_msg_title)
    TextView tvMsgTitle;
    @BindView(R.id.tv_msg_content)
    TextView tvMsgContent;
    @BindView(R.id.iv_goods)
    ImageView ivGoods;
    @BindView(R.id.iv_pass)
    ImageView ivPass;
    @BindView(R.id.tv_good_title)
    TextView tvGoodTitle;
    @BindView(R.id.rv_good_list)
    LRecyclerView rvGoodList;
    @BindView(R.id.tv_source_code_title)
    TextView tvSourceCodeTitle;
    @BindView(R.id.tv_source_code_content)
    TextView tvSourceCodeContent;
    @BindView(R.id.ll_source_code)
    LinearLayout llSourceCode;
    @BindView(R.id.tv_goods_date_title)
    TextView tvGoodsDateTitle;
    @BindView(R.id.tv_goods_date_content)
    TextView tvGoodsDateContent;
    @BindView(R.id.ll_goods_date)
    LinearLayout llGoodsDate;

    private ADA_WarehouseGoodsList_Out adapter;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private String id;

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        id = extras.getString("id");
    }

    @Override
    protected void initEventAndData() {

        mPresenter.getDetail(id);
        adapter = new ADA_WarehouseGoodsList_Out(mContext);
        lRecyclerViewAdapter = new LRecyclerViewAdapter(adapter);
        final LinearLayoutManager manager = new LinearLayoutManager(mContext);
        rvGoodList.setLayoutManager(manager);
        rvGoodList.setAdapter(lRecyclerViewAdapter);
        rvGoodList.setLoadMoreEnabled(false);
        rvGoodList.setPullRefreshEnabled(false);
        SpacesItemDecoration divider = SpacesItemDecoration.newInstance(0,
                DisplayUtil.dip2px(mContext, 10), 1, getResources().getColor(R.color.transparent));
        rvGoodList.addItemDecoration(divider);
    }

    @Override
    protected int getLayout() {
        return R.layout.act_warehouse_out_detail;
    }

    @Override
    public void showToast(String msg) {
        PosToast.INSTANCE.makeShortToast(mContext, msg);
    }

    @Override
    public void onDetailReceived(OutWarehouseDetailEntity entity) {
        adapter.update(entity.getGoodsList());

        tvOrder.setText("已生成的" + getInType(entity.getOutType()) + "单");
        tvName.setText(entity.getUserName());

        tvTicketNum.setText(entity.getOutCode());
        tvWarehouseTypeContent.setText(getInType(entity.getOutType()));
        tvWarehouseContent.setText(entity.getWarehouseName());
        tvDateContent.setText(AppDateUtil.getTimeStamp(entity.getCreateTime(),AppDateUtil.YYYY_MM_DD_HH_MM));
        tvMsgContent.setText(entity.getRemark());
    }

    private String getInType(String inType) {
        if ("0".equals(inType))
            return "其他出库";
        if ("2".equals(inType))
            return "盘亏出库";
        if ("5".equals(inType))
            return "报损出库";
        return "错误类型";
    }

    public static void startAct(Context context, String id) {
        Intent intent = new Intent(context, ACT_WarehouseDetail_Out.class);
        Bundle bundle = new Bundle();

        bundle.putString("id", id);

        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
